package com.oracle.cmp.service;

import com.oracle.cmp.common.Constant;
import com.oracle.cmp.common.Result;
import com.oracle.cmp.entity.PartsRepertory;
import com.oracle.jdbc.util.Dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PartsRepetoryImpl implements PartsRepoertoryService {
    @Override
    public PartsRepertory selectById(int PartsId) {
        String sql="select PartsReqCount FROM biz_partsrepertory where PartsId=?";
        return Dao.queryOne(sql,PartsRepertory.class,new Object[]{PartsId});
    }

    @Override
    public Result save(PartsRepertory partsRepertory) {
        return null;
    }

    @Override
    public Result queryForPage(Map<String, Object> map, int pageNum) {
        String sql = "select  r.PartsRepId,r.PartsId,PartsName,PartsReqCount from biz_partsrepertory r " +
                "left outer join biz_parts p on r.PartsId=p.PartsId where 1=1";
        List params = new ArrayList();
        if(map.get("partsName")!=null&&!"".equals(map.get("partsName"))){
            sql += " and PartsName = ?";
            params.add(map.get("partsName"));
        }
        if(map.get("partsId")!=null&&!"".equals(map.get("partsId"))){
            sql += " and r.PartsId = ?";
            params.add(map.get("partsId"));
        }
        sql+=" limit ?,?";

        params.add((pageNum-1)* Constant.PAGESIZE);
        params.add(Constant.PAGESIZE);
        List<PartsRepertory> list = Dao.query(sql,PartsRepertory.class,params.toArray());
        return new Result(true,list,"查询成功");
    }

    @Override
    public int count(Map<String, Object> map) {
        String sql = "SELECT COUNT(0) FROM (select r.PartsId,PartsName from biz_partsrepertory r left outer join biz_parts p on r.PartsId=p.PartsId WHERE 1=1";
        List params = new ArrayList();
        if(map.get("partsName")!=null&&!"".equals(map.get("partsName"))){
            sql += " and PartsName = ?";
            params.add(map.get("partsName"));
        }
        if(map.get("partsId")!=null&&!"".equals(map.get("partsId"))){
            sql += " and r.PartsId = ?";
            params.add(map.get("partsId"));
        }
        sql+=") AS A";
        int total = ((Long)Dao.queryOne(sql,params.toArray())[0]).intValue();
        return total;
    }

}
